import yaml

class Object:
    def __init__(self):
        self.vars = {}
        self.hasVar = self.vars.has_key
        self.getVar = self.vars.__getitem__
        
    def load(self, filename):
        try:
            yamlDict = yaml.load(open("dat/" + filename))
        except:
            return False
        
        self.vars.update(self.evalDict(yamlDict))
        
        print "loaded", filename
        
        return True
    
    def evalDict(self, d):
        newd = {}
        for key, val in d.iteritems():
            if type(val) == dict:
                newd[key] = self.evalDict(val)
            else:
                try:
                    newd[key] = eval(val)
                except:
                    newd[key] = val
        return newd
                
    def destroy(self):
        pass
        
    def save(self, filename):
        print filename, self.vars
        yaml.dump(self.vars,open("dat/" + filename,"w"))
    
    def setVar(self, name, val):
        self.vars[name] = val
        
    #def getVar(self, name):
    #    if self.hasVar(name):
    #        return self.vars[name]
    #    else:
    #        return None
        
    #def hasVar(self, name):
    #    return self.vars.has_key(name)
        
    def defaultVar(self, name, val):
        if not self.hasVar(name):
            self.setVar(name,val)
            
    def removeVar(self, name):
        if self.hasVar(name):
            return self.vars.pop(name)
        else:
            return None
